package com.honeymoondesert.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternHelper {
	public static void main(String[] args) {
		System.out.println(PatternHelper.getIntNum("平均价格:170元"));
		System.out.println(PatternHelper.getDoubleNum("环境:7.1"));
	}
	public static Integer getIntNum(String str) {
		if(str==null)return null;
		String regEx="\\d+";   //正则表达式
		Pattern p = Pattern.compile(regEx);   
		Matcher m = p.matcher(str);
		String result=null;
		while (m.find()) {
			result=m.group();
		}
		Integer iResult=result==null?null:Integer.parseInt(result);
		return iResult;
	}
	public static Double getDoubleNum(String str) {
		if(str==null)return null;
		String regEx="\\d+\\.\\d+";   //正则表达式
		Pattern p = Pattern.compile(regEx);   
		Matcher m = p.matcher(str);
		String result=null;
		while (m.find()) {
			result=m.group();
		}
		Double dResult=result==null?null:Double.parseDouble(result);
		return dResult;
	}
	public static String  getStrAsRegx(String str,String regex){
		Pattern p=Pattern.compile(regex);
		Matcher m = p.matcher(str);
		String result=null;
		if (m.find()) {
			result=m.group();
		}
		return result;
	}
}
